<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>斐波那契和递归求和</title>
</head>
<body>
  <p>请输入大于0的整数</p>
  <div class="box">
    <input type="number"/>
    <button id="fibo" onclick="clickFibo()">斐波那契</button>
    <button id="sum" onclick="clickSum()">求和</button>
    <p id="result"></p>
  </div>
  <script>
    const input = document.querySelector('input'),
      result = document.querySelector('#result');
    function clickFibo() {
      const value = Number(input.value)
      if(!value) {
        alert('请输入数字')
        return
      } else {
        result.innerHTML = getFibonacci(value)
      }
    }
    function clickSum() {
      const value = Number(input.value)
      if(!value) {
        alert('请输入数字')
        return
      } else {
        result.innerHTML = getSum(value)
      }
    }
    // 斐波那契数列 1,1,2,3,5,8,13,21
    function getFibonacci(num){
      let fibonacci = [1, 1];
      for(let i = 2; i < num; i++) {
        fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]
      }
      return fibonacci
    }
    console.log('getFibonacci(10)', getFibonacci(10));
    // 求和1 - n
    function getSum(num) {
      if(num == 1) {
        return 1
      } else {
        return getSum(num-1) + num
      }
    }
    console.log('getSum(10)', getSum(10));
  </script>
</body>
</html>